/*
 * @Author: WangGuojian 1085844536@qq.com
 * @Date: 2023-04-02 21:20:22
 * @LastEditTime: 2023-04-04 22:23:55
 * @LastEditors: WangGuojian 1085844536@qq.com
 * @FilePath: \webpack\webpack_code\src\main.js
 * @Description:
 */
// 完整引入
// import 'core-js';
// 按需加载
// import 'core-js/es/promise';

import count from './js/count';
import sum from './js/sum';

// 想要webpack打包资源，必须引入该资源
import './css/iconfont.css';
import './css/index.css';
import './less/index.less';
import './sass/index.sass';
import './sass/index.scss';
import './stylus/index.styl';

let result = count(2, 2);
console.log(result);
console.log(sum(1, 2, 3, 4));

document.getElementById('btn').onclick = function () {
    // eslint 不能识别动态导入，需要额外追加配置
    // /* webpackChunkName: "math" */ webpack 魔法命名
    import(/* webpackChunkName: "math" */ './js/math').then(({ mul }) => {
        console.log(mul(3, 3));
    });
};

if (module.hot) {
    // 判断是否支持热模块替换功能
    module.hot.accept('./js/count');
    module.hot.accept('./js/sum');
}

new Promise((resolve) => {
    setTimeout(() => {
        resolve();
    }, 1000);
});

const arr = [1, 2, 3, 4];
console.log(arr.includes(1));

if ('serviceWorker' in navigator) {
    window.addEventListener('load', () => {
        navigator.serviceWorker
            .register('/service-worker.js')
            .then((registration) => {
                console.log('SW registration: ', registration);
            })
            .catch((registrationError) => {
                console.log('SW registration error: ', registrationError);
            });
    });
}
